package com.kd.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * ****************************************. 与date相关操作的辅助工具类.
 *
 * @author Qiang.Liu
 * @create on 2008-11-26 上午11:27:49 ****************************************
 */
public class DateUtils {
	private static final String default_pattern = "yyyyMMdd";

	/**
	 * New gmt gregorian calendar.
	 *
	 * @return the gregorian calendar
	 */
	public static GregorianCalendar newGMTGregorianCalendar() {
		return new GregorianCalendar(TimeZone.getTimeZone("GMT+0"));
	}

	/**
	 * New gmt xml gregorian calendar.
	 *
	 * @return the xML gregorian calendar
	 */
	public static XMLGregorianCalendar newGmtXMLGregorianCalendar() {
		try {
			return DatatypeFactory.newInstance().newXMLGregorianCalendar(
					new GregorianCalendar(TimeZone.getTimeZone("GMT+0")));
		} catch (DatatypeConfigurationException ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * New xml gregorian calendar.
	 *
	 * @return the xML gregorian calendar
	 */
	public static XMLGregorianCalendar newXMLGregorianCalendar() {
		return newXMLGregorianCalendar(new GregorianCalendar());
	}

	/**
	 * New xml gregorian calendar.
	 *
	 * @param gregorianCalendar
	 *            the gregorian calendar
	 *
	 * @return the xML gregorian calendar
	 */
	public static XMLGregorianCalendar newXMLGregorianCalendar(
			GregorianCalendar gregorianCalendar) {
		if (gregorianCalendar != null) {
			try {
				return DatatypeFactory.newInstance().newXMLGregorianCalendar(
						gregorianCalendar);
			} catch (DatatypeConfigurationException ex) {
				ex.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * New gmt xml gregorian calendar.
	 *
	 * @param datetime
	 *            the datetime
	 *
	 * @return the xML gregorian calendar
	 */
	public static XMLGregorianCalendar newGmtXMLGregorianCalendar(Date datetime) {
		if (datetime != null) {
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(datetime);
			return newXMLGregorianCalendar(calendar);
		}
		return null;
	}

	/**
	 * Xml gregorian calendar2 date.
	 *
	 * @param xmlGregorianCalendar
	 *            the xml gregorian calendar
	 *
	 * @return the date
	 */
	public static Date xmlGregorianCalendar2Date(
			XMLGregorianCalendar xmlGregorianCalendar) {
		if (xmlGregorianCalendar == null
				|| xmlGregorianCalendar.toGregorianCalendar() == null) {
			return null;
		}
		return xmlGregorianCalendar.toGregorianCalendar().getTime();
	}

	/**
	 * Format.
	 *
	 * @param meterDateTime
	 *            the meter date time
	 * @param pattern
	 *            the pattern
	 *
	 * @return the string
	 */
	public static String format(XMLGregorianCalendar meterDateTime,
			String pattern) {
		Date date = xmlGregorianCalendar2Date(meterDateTime);
		if (date != null) {
			return format(date, pattern);
		}
		return null;
	}
	public static String format(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	public static String format(long millis, String pattern) {
		return format(new Date(millis), pattern);
	}

	public static Date parse(String stringDate, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = sdf.parse(stringDate);
		} catch (ParseException e) {
			throw new RuntimeException(e.getMessage());
		}
		return date;
	}

	public static Long parseLong(String date, String pattern) {
		Date d = parse(date, pattern);
		if (d != null) {
			return d.getTime();
		}
		return null;
	}

	

	/**
	 * Checks if is time out.
	 *
	 * @param sent
	 *            the sent
	 * @param timeToLive
	 *            the time to live
	 *
	 * @return true, if is time out
	 */
	public static boolean isTimeOut(XMLGregorianCalendar sent, int timeToLive) {
		if (sent == null) {
			return true;
		}
		return timeToLive != 0
				&& sent.toGregorianCalendar().getTime().before(
						new Date(System.currentTimeMillis() - timeToLive));
	}

	/**
	 * Adds the years.
	 *
	 * @param date
	 *            the date
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date addYears(Date date, int amount) {
		return add(date, Calendar.YEAR, amount);
	}

	/**
	 * Adds the months.
	 *
	 * @param date
	 *            the date
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date addMonths(Date date, int amount) {
		return add(date, Calendar.MONTH, amount);
	}

	/**
	 * Adds the weeks.
	 *
	 * @param date
	 *            the date
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date addWeeks(Date date, int amount) {
		return add(date, Calendar.WEEK_OF_YEAR, amount);
	}

	/**
	 * Adds the days.
	 *
	 * @param date
	 *            the date
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date addDays(Date date, int amount) {
		return add(date, Calendar.DAY_OF_MONTH, amount);
	}

	/**
	 * Adds the hours.
	 *
	 * @param date
	 *            the date
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date addHours(Date date, int amount) {
		return add(date, Calendar.HOUR_OF_DAY, amount);
	}

	/**
	 * Adds the minutes.
	 *
	 * @param date
	 *            the date
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date addMinutes(Date date, int amount) {
		return add(date, Calendar.MINUTE, amount);
	}

	/**
	 * Adds the seconds.
	 *
	 * @param date
	 *            the date
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date addSeconds(Date date, int amount) {
		return add(date, Calendar.SECOND, amount);
	}

	/**
	 * Adds the milliseconds.
	 *
	 * @param date
	 *            the date
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date addMilliseconds(Date date, int amount) {
		return add(date, Calendar.MILLISECOND, amount);
	}

	/**
	 * Adds the.
	 *
	 * @param date
	 *            the date
	 * @param calendarField
	 *            the calendar field
	 * @param amount
	 *            the amount
	 *
	 * @return the date
	 */
	public static Date add(Date date, int calendarField, int amount) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(calendarField, amount);
		return c.getTime();
	}

	public static int getDay(Date date) {
		return get(date, Calendar.DAY_OF_MONTH);
	}

	public static int get(Date date, int calendarField) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(calendarField);
	}

	/**
	 * Truncate.
	 *
	 * @param date
	 *            the date
	 * @param field
	 *            the field
	 *
	 * @return the date
	 */
	public static Date truncate(Date date, int field) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar gval = Calendar.getInstance();
		gval.setTime(date);
		modify(gval, field, false);
		return gval.getTime();
	}

	/**
	 * <p>
	 * Truncate this date, leaving the field specified as the most significant
	 * field.
	 * </p>
	 *
	 * <p>
	 * For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you
	 * passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was
	 * passed with MONTH, it would return 1 Mar 2002 0:00:00.000.
	 * </p>
	 *
	 * @param date
	 *            the date to work with
	 * @param field
	 *            the field from <code>Calendar</code> or
	 *            <code>SEMI_MONTH</code>
	 *
	 * @return the rounded date (a different object)
	 *
	 * @throws IllegalArgumentException
	 *             if the date is <code>null</code>
	 * @throws ArithmeticException
	 *             if the year is over 280 million
	 */
	public static Calendar truncate(Calendar date, int field) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar truncated = (Calendar) date.clone();
		modify(truncated, field, false);
		return truncated;
	}

	/**
	 * <p>
	 * Truncate this date, leaving the field specified as the most significant
	 * field.
	 * </p>
	 *
	 * <p>
	 * For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you
	 * passed with HOUR, it would return 28 Mar 2002 13:00:00.000. If this was
	 * passed with MONTH, it would return 1 Mar 2002 0:00:00.000.
	 * </p>
	 *
	 * @param date
	 *            the date to work with, either <code>Date</code> or
	 *            <code>Calendar</code>
	 * @param field
	 *            the field from <code>Calendar</code> or
	 *            <code>SEMI_MONTH</code>
	 *
	 * @return the rounded date
	 *
	 * @throws IllegalArgumentException
	 *             if the date is <code>null</code>
	 * @throws ClassCastException
	 *             if the object type is not a <code>Date</code> or
	 *             <code>Calendar</code>
	 * @throws ArithmeticException
	 *             if the year is over 280 million
	 */
	public static Date truncate(Object date, int field) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		if (date instanceof Date) {
			return truncate((Date) date, field);
		} else if (date instanceof Calendar) {
			return truncate((Calendar) date, field).getTime();
		} else {
			throw new ClassCastException("Could not truncate " + date);
		}
	}

	// -----------------------------------------------------------------------
	/**
	 * <p>
	 * Internal calculation method.
	 * </p>
	 *
	 * @param val
	 *            the calendar
	 * @param field
	 *            the field constant
	 * @param round
	 *            true to round, false to truncate
	 *
	 * @throws ArithmeticException
	 *             if the year is over 280 million
	 */
	private static void modify(Calendar val, int field, boolean round) {
		if (val.get(Calendar.YEAR) > 280000000) {
			throw new ArithmeticException(
					"Calendar value too large for accurate calculations");
		}

		if (field == Calendar.MILLISECOND) {
			return;
		}

		// ----------------- Fix for LANG-59 ---------------------- START
		// ---------------
		// see http://issues.apache.org/jira/browse/LANG-59
		//
		// Manually truncate milliseconds, seconds and minutes, rather than
		// using
		// Calendar methods.

		Date date = val.getTime();
		long time = date.getTime();
		boolean done = false;

		// truncate milliseconds
		int millisecs = val.get(Calendar.MILLISECOND);
		if (!round || millisecs < 500) {
			time = time - millisecs;
			if (field == Calendar.SECOND) {
				done = true;
			}
		}

		// truncate seconds
		int seconds = val.get(Calendar.SECOND);
		if (!done && (!round || seconds < 30)) {
			time = time - (seconds * 1000L);
			if (field == Calendar.MINUTE) {
				done = true;
			}
		}

		// truncate minutes
		int minutes = val.get(Calendar.MINUTE);
		if (!done && (!round || minutes < 30)) {
			time = time - (minutes * 60000L);
		}

		// reset time
		if (date.getTime() != time) {
			date.setTime(time);
			val.setTime(date);
		}
		// ----------------- Fix for LANG-59 ----------------------- END
		// ----------------

		boolean roundUp = false;
		for (int i = 0; i < fields.length; i++) {
			for (int j = 0; j < fields[i].length; j++) {
				if (fields[i][j] == field) {
					// This is our field... we stop looping
					if (round && roundUp) {
						if (field == SEMI_MONTH) {
							// This is a special case that's hard to generalize
							// If the date is 1, we round up to 16, otherwise
							// we subtract 15 days and add 1 month
							if (val.get(Calendar.DATE) == 1) {
								val.add(Calendar.DATE, 15);
							} else {
								val.add(Calendar.DATE, -15);
								val.add(Calendar.MONTH, 1);
							}
						} else {
							// We need at add one to this field since the
							// last number causes us to round up
							val.add(fields[i][0], 1);
						}
					}
					return;
				}
			}
			// We have various fields that are not easy roundings
			int offset = 0;
			boolean offsetSet = false;
			// These are special types of fields that require different rounding
			// rules
			switch (field) {
			case SEMI_MONTH:
				if (fields[i][0] == Calendar.DATE) {
					// If we're going to drop the DATE field's value,
					// we want to do this our own way.
					// We need to subtrace 1 since the date has a minimum of 1
					offset = val.get(Calendar.DATE) - 1;
					// If we're above 15 days adjustment, that means we're in
					// the
					// bottom half of the month and should stay accordingly.
					if (offset >= 15) {
						offset -= 15;
					}
					// Record whether we're in the top or bottom half of that
					// range
					roundUp = offset > 7;
					offsetSet = true;
				}
				break;
			case Calendar.AM_PM:
				if (fields[i][0] == Calendar.HOUR_OF_DAY) {
					// If we're going to drop the HOUR field's value,
					// we want to do this our own way.
					offset = val.get(Calendar.HOUR_OF_DAY);
					if (offset >= 12) {
						offset -= 12;
					}
					roundUp = offset > 6;
					offsetSet = true;
				}
				break;
			}
			if (!offsetSet) {
				int min = val.getActualMinimum(fields[i][0]);
				int max = val.getActualMaximum(fields[i][0]);
				// Calculate the offset from the minimum allowed value
				offset = val.get(fields[i][0]) - min;
				// Set roundUp if this is more than half way between the minimum
				// and maximum
				roundUp = offset > ((max - min) / 2);
			}
			// We need to remove this field
			if (offset != 0) {
				val.set(fields[i][0], val.get(fields[i][0]) - offset);
			}
		}
		throw new IllegalArgumentException("The field " + field
				+ " is not supported");

	}

	/** The Constant MILLIS_PER_SECOND. */
	public static final long MILLIS_PER_SECOND = 1000;

	/** The Constant MILLIS_PER_MINUTE. */
	public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;

	/** The Constant MILLIS_PER_HOUR. */
	public static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;

	/** The Constant MILLIS_PER_DAY. */
	public static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;

	/** The Constant SEMI_MONTH. */
	public final static int SEMI_MONTH = 1001;

	/** The Constant fields. */
	private static final int[][] fields = { { Calendar.MILLISECOND },
			{ Calendar.SECOND }, { Calendar.MINUTE },
			{ Calendar.HOUR_OF_DAY, Calendar.HOUR },
			{ Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.AM_PM
			/*
			 * Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK,
			 * Calendar.DAY_OF_WEEK_IN_MONTH
			 */
			}, { Calendar.MONTH, SEMI_MONTH }, { Calendar.YEAR },
			{ Calendar.ERA } };

	/**
	 * Checks if is expire.
	 *
	 * @param expireDateTime
	 *            the expire date time
	 *
	 * @return true, if is expire
	 */
	public static boolean isExpire(Date expireDateTime) {
		return new Date().after(expireDateTime);
	}

	public static Date setDay(int day) {
		return setDay(new Date(), day);
	}

	public static Date setDay(Date date, int day) {
		return set(date, Calendar.DAY_OF_MONTH, day);
	}

	public static int getMaximum(Date date, int field) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getMaximum(field);
	}

	public static Date set(Date date, int field, int value) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int maximum = c.getActualMaximum(field);
		if (value > maximum) {
			value = maximum;
		}
		c.set(field, value);
		return c.getTime();
	}

	public static boolean validateFormat(String date, String pattern) {
		return parse(date, pattern) != null;
	}

	public static String formatIfNotNull(Date date) {
		return formatIfNotNull(date, default_pattern);
	}

	public static String formatIfNotNull(Date date, String pattern) {
		if (date != null) {
			return format(date, pattern);
		}
		return null;
	}

	public static String changePattern(String date, String fromPattern,
			String toPattern) {
		if (StringUtils.isBlank(date))
			return date;
		return format(parse(date, fromPattern), toPattern);
	}

	

	public static String format(Date sysdate) {
		return formatIfNotNull(sysdate);
	}

	public static String getSysdate(String pattern) {
		return format(new Date(), pattern);
	}

	public static String addDays(String date, int amount, String pattern)
			throws ParseException {
		return format(addDays(parse(date, pattern), amount), pattern);
	}

	public static boolean equals(Date ori, Date dist) {
		if (ori == null || dist == null) {
			if (ori == null && dist == null) {
				return true;
			}
			return false;
		}
		return ori.getTime() == dist.getTime();
	}

	// 将dest时间的fields，替换成ori的fields,fields都是Calendar.的常�?
	public static Date replace(Date dest, Date ori, int... fields) {
		Calendar c = Calendar.getInstance();
		c.setTime(dest);
		for (int field : fields) {
			c.set(field, get(ori, field));
		}
		return c.getTime();
	}

	public static void main1(String[] args) throws ParseException {
		// new SimpleDateFormat("yyyyMMdd").parse(string);
		System.out.println(changePattern("100101", "yyMMdd", "yyyyMMdd"));
		// System.out.println(changePattern("   "));
		// System.out.println(isBeforeToday("0990403", "eeeMMdd"));
		// System.out.println(DateUtils.changePattern("1212", "MMdd",
		// "yyyyMMdd"));
		// System.out.println(yyyyMMdd2eeeMMdd("20100101"));
		// System.out.println();
		// Date begin = DateUtils.parse("0000", "HHmm");
		// System.out.println(DateUtils.format(replace(new
		// Date(),begin,Calendar.
		// HOUR_OF_DAY,Calendar.MINUTE),"yyyy/MM/dd HH:mm:ss"));
	}

	public static Date truncateSysdate(int field) {
		return truncate(new Date(), field);
	}

	public static String yyyyMMdd2eeeMMdd(String date) {
		return DateUtils.changePattern(date, "yyyyMMdd", "eeeMMdd");
	}

	public static String eeeMMdd2yyyyMMdd(String date) {
		return DateUtils.changePattern(date, "eeeMMdd", "yyyyMMdd");
	}

	public static String yyyyMM2eeeMM(String date) {
		return DateUtils.changePattern(date, "yyyyMM", "eeeMM");
	}

	public static boolean isAfterToday(String date, String pattern) {
		Date sysdate = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
		Date datetime = DateUtils.parse(date, pattern);
		return (datetime != null && datetime.after(sysdate));
	}

	public static boolean isBeforeToday(String date, String pattern) {
		Date sysdate = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
		Date datetime = DateUtils.parse(date, pattern);
		return (datetime != null && datetime.before(sysdate));
	}

	public static String getyyyyMMdd(Date revtime) {
		return format(revtime, "yyyyMMdd");
	}

	public static String getHHmmss(Date revtime) {
		return format(revtime, "HHmmss");
	}

	public static java.sql.Date convertToSqlDate(Date date) {
		if (date == null)
			return null;
		return new java.sql.Date(date.getTime());
	}

	public static String getSysdateyyyyMMdd() {
		return getSysdate("yyyyMMdd");
	}

	public static String getYesterdayYYYYMMDD() {
		return format(addDays(new Date(), -1), "yyyyMMdd");
	}

	public static String getYesterdayEEEMMDD() {
		return format(addDays(new Date(), -1), "eeeMMdd");
	}

	public static String eeeMM2yyyyMM(String date) {
		return DateUtils.changePattern(date, "eeeMM", "yyyyMM");
	}

	public static String formatDuration(String duration) {
		String strRet = "";
		try {
			int iTime = Integer.parseInt(duration);
			int hour = iTime / 3600;
			int minute = (iTime / 60) % 60;
			int second = iTime % 60;
			if (hour != 0)
				strRet = strRet.format("%1$,02d:%2$,02d:%3$,02d", hour, minute,second);
			else {
				strRet = strRet.format("%1$,02d:%2$,02d", minute, second);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return duration;
		}
		return strRet;
	}

	public static Long FormString(String str) {
		Long times = Long.parseLong("0");
		if (str == null || str.trim().length() == 0) {
			return 0l;
		}
		str = str.replace("年", "-");
		str = str.replace("月", "-");
		str = str.replace("日", " ");
		str = str.replace(",", "-");
		SimpleDateFormat format = null;
		if (str.indexOf(":") != -1) {
			format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		} else {
			format = new SimpleDateFormat("yyyy-MM-dd");
		}
		Date parse = null;
		try {
			parse = format.parse(str);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
		if (parse != null) {
			int year = parse.getYear() + 1900;
			if (year <= (new Date().getYear() + 1900)) {
				times = parse.getTime();
			} else {
//				return Long.parseLong("0");
			}
		}
		return times;
	}




	 /**
	     * 阿拉伯数字
	     */
	    private static Set<Character> araDigits = new HashSet<Character>();
	    /**
	     * 汉字中的数字字符
	     */
	    private static char[] SCDigits = {'零','一','二','三','四','五','六','七','八','九','十','百','千','万','亿'};

	    /**
	     * 汉字中的大写数字字符
	     */
	    private static char[] TCDigits = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖','拾','佰','仟','万','亿'};
	    /**
	     * 繁体中文和简体中文的对应关系
	     */
	    private static Map<Character,Character> map = new HashMap<Character,Character>();
	    static {
	        for (int i = 0; i < TCDigits.length; i++) {
	            map.put(TCDigits[i], SCDigits[i]);
	        }
	        for (char i = '0'; i <= '9'; i++) {
	            araDigits.add(i);
	        }
	    }

	    /**
	     * 解析中文格式的数字，假定参数中全是汉字，否则会解析异常，解析失败返回null
	     * @param hanzi
	     * @return
	     */
	    public static Integer parseDigits(String hanzi) {
	        if (!isDigits(hanzi))
	            return null;
	        int ret;
	        try {
	            if (hanzi.charAt(0) == '+')
	                hanzi = hanzi.substring(1);

	            ret = Integer.parseInt(hanzi);
	        } catch (Exception e) {

	            char[] chars = hanzi.toCharArray();
	            changeTCtoSC(chars);

	            ret = parse(chars,0,chars.length,1);
	        }

	        return ret;
	    }
	    public static boolean isDigits(String s) {
	        if (s.charAt(0) == '+')
	            s = s.substring(1);
	        try {
	            Integer.parseInt(s);
	            return true;
	        } catch (Exception e) {
	            for (int i = 0; i < s.length(); i++) {
	                char c = s.charAt(i);
	                if (!map.values().contains(c) && !araDigits.contains(c))
	                    return false;
	            }

	            return true;
	        }
	    }
	    private static int parse(char[] chars,int start,int end, int preNumber) {
	        int ret = 0;
	        if (start == end) {
	            ret = 0;
	        } else if (start + 1 == end) {
	            switch (chars[start]) {
	            case '一':
	            case '1':
	                ret = 1 * preNumber;
	                break;
	            case '二':
	            case '2':
	                ret = 2 * preNumber;
	                break;
	            case '三':
	            case '3':
	                ret = 3 * preNumber;
	                break;
	            case '四':
	            case '4':
	                ret = 4 * preNumber;
	                break;
	            case '五':
	            case '5':
	                ret = 5 * preNumber;
	                break;
	            case '六':
	            case '6':
	                ret = 6 * preNumber;
	                break;
	            case '七':
	            case '7':
	                ret = 7 * preNumber;
	                break;
	            case '八':
	            case '8':
	                ret = 8 * preNumber;
	                break;
	            case '九':
	            case '9':
	                ret = 9 * preNumber;
	                break;
	            }
	        } else {
	            int index;
	            if ((index = indexOf(chars,start,end,'零')) == 0 || (index = indexOf(chars,start,end,'0')) == 0) {
	                ret = parse(chars, start + 1, end, 1);
	            } else if ((index = indexOf(chars,start,end,'亿')) != -1) {
	                ret = parse(chars, start,index, 1) * 100000000 + parse(chars,index + 1,end,10000000);
	            } else if ((index = indexOf(chars,start,end,'万')) != -1) {
	                ret = parse(chars, start,index, 1) * 10000 + parse(chars,index + 1,end,1000);
	            } else if ((index = indexOf(chars,start,end,'千')) != -1) {
	                ret = parse(chars, start, index, 1) * 1000 + parse(chars,index + 1,end,100);
	            } else if ((index = indexOf(chars,start,end,'百')) != -1) {
	                ret = parse(chars, start, index, 1) * 100 + parse(chars,index + 1,end,10);
	            } else if ((index = indexOf(chars,start,end,'十')) != -1) {
	                ret = parse(chars, start, index, 1) * 10 + parse(chars,index + 1,end,1);
	            }

	        }
	        return ret;
	    }
	    private static int indexOf(char[] chars, int start, int end, char c) {
	        for (int i = start; i < end; i++) {
	            if (chars[i] == c)
	                return i;
	        }
	        return -1;
	    }
	    /**
	     * 将繁体中文转换为简体中文
	     * @param chars
	     */
	    private static void changeTCtoSC(char[] chars) {
	        for (int i = 0; i < chars.length; i++) {
	            Character c = map.get(chars[i]);
	            if (c != null)
	                chars[i] = c;
	        }
	    }

	    private final static String YEAR_2_4 = "(19[0-9]{2}|20[0-9]{2}|[0-9]{2})";
	    private final static String YEAR_4 = "(19[0-9]{2}|20[0-9]{2})";
	    private final static String MONTH_1_2 = "(0[1-9]|1[012]|[1-9])";
	    private final static String MONTH_2 = "(0[1-9]|1[012])";
	    private final static String DAY_1_2 = "(0[1-9]|[12][0-9]|3[01]|[1-9])";
	    private final static String DAY_2 = "(0[1-9]|[12][0-9]|3[01])";
	    private final static String HOUR = "(0[0-9]|1[0-9]|2[0-3]|[0-9])";
	    private final static String MIN = "([0-5][0-9]|[0-9])";
	    private final static String SECOND = "([0-5][0-9]|[0-9])";

		//刚刚，1天前，1秒前，1星期前等
		private final static Pattern P_DATE_CHINESE = Pattern.compile("(刚刚|([0-9]{1,2}|半)[\\D]*([秒天周月年分]|分钟|小时|星期|个月|月)前|[昨今前]天)");

		private final static Pattern P_DATE_CHINESE_SPECIAL=Pattern.compile("([0-9]{1,2})小时([0-9]{1,2})分钟前");

		//二〇〇八年三月三十一日，二〇一二年十二月二十六日
		private final static String CHINESE_NUMBER = "零0○O〇壹一贰二叁三肆四伍五陆六柒七捌八玖九拾十佰百仟千";
		private final static Pattern DATE_CHINESE = Pattern.compile(String.format("([%s]{2,4})年([%s]{1,3})月([%s]{1,3})[日]?",CHINESE_NUMBER,CHINESE_NUMBER,CHINESE_NUMBER));

		private final static String[] NUMBER = {"零","一","二","三","四","五","六","七","八","九"};

	/**
	 * 通用智能日期解析
	 * @param dateStr
	 * @return
	 */
	public static Long matchDate(String dateStrOri){

		if(StringUtils.isNullOrEmpty(dateStrOri)){
			return null;
		}

		try {
			Integer year = null;
			int month = 0;
			int day = 0;
			Integer hour = null;
			Integer min = null;
			Integer sec = null;
			Calendar cal = Calendar.getInstance();


			Matcher matcher = null;

			if ((matcher = DATE_CHINESE.matcher(dateStrOri)).find()){
				//二〇〇八年三月三十一日，二〇一二年十二月二十六日
				String yearStr = matcher.group(1);
				String monthStr = matcher.group(2);
				String dayStr = matcher.group(3);
				year = ChineseNumberParser.parseChineseNumber(yearStr);
				month = ChineseNumberParser.parseChineseNumber(monthStr);
				day = ChineseNumberParser.parseChineseNumber(dayStr);
				if (year==-1 || month==-1 || day==-1){
					System.out.println("中文日期解析错误：" + yearStr + "年" +monthStr +"月"+dayStr + "日"  );
					return null;
				}
			}else{
			String dateStr = formatMatchDateString(dateStrOri);

			if ((matcher = pattern(DAY_1_2+"[月| |-]"+MONTH_1_2+"[日|-]"+YEAR_2_4 +"[^0-9]{0,3}(AM|PM)[^0-9]{0,3}"+ HOUR + "[:|时|点]" + MIN + "[:|分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
				//21/1/2013 PM 4:36:45　
				day = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				String ampm = matcher.group(4);
				hour = NumberUtils.parseInt(matcher.group(5));
				min = NumberUtils.parseInt(matcher.group(6));
				sec = NumberUtils.parseInt(matcher.group(7));
				if ("PM".equals(ampm)){
					hour = 12 + hour;
				}
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,3}(上午|下午)[^0-9]{0,3}" + HOUR + "[:时|点]" + MIN + "[分]?").matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				String sxw = matcher.group(4);
				hour = NumberUtils.parseInt(matcher.group(5));
				min = NumberUtils.parseInt(matcher.group(6));
				if ("下午".equals(sxw)){
					hour = 12 + hour;
				}
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,10}"+ HOUR + "[:|时|点]" + MIN + "[:|分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
//			14-7-6 12:26:21
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
				sec = NumberUtils.parseInt(matcher.group(6));
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,10}" + HOUR + "[:时|点]" + MIN + "[分]?").matcher(dateStr)).find()){
//			14-7-6 12:26
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern(YEAR_4 +"[年| |-]?"+MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?[^0-9]{0,10}"+ HOUR + "[:|时|点]" + MIN + "[:|分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
//			201476 12:26:01
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
				sec = NumberUtils.parseInt(matcher.group(6));
			}else if ((matcher = pattern("上午"+ HOUR + "[:|时|点]" + MIN + "[:|分]?" +"[^0-9]{0,10}"+YEAR_4 +"[年| |-]?"+MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
//				上午8:35 - 2015年10月12日
				hour = NumberUtils.parseInt(matcher.group(1));
				min = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				month = NumberUtils.parseInt(matcher.group(4));
				day = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern("下午"+ HOUR + "[:|时|点]" + MIN + "[:|分]?" +"[^0-9]{0,10}"+YEAR_4 +"[年| |-]?"+MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
//				上午8:35 - 2015年10月12日
				hour = NumberUtils.parseInt(matcher.group(1));
				hour = hour+12;
				min = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				month = NumberUtils.parseInt(matcher.group(4));
				day = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern(YEAR_4 +"[年| |-]?"+MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?[^0-9]{0,10}"+ HOUR + "[:|时|点]" + MIN ).matcher(dateStr)).find()){
//			201476 12:26
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日-]" +YEAR_4 +"[^0-9]{0,10}"+ HOUR + "[:时|点]" + MIN + "[:分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
//			7/6/2014 12:26:01
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
				sec = NumberUtils.parseInt(matcher.group(6));
			}else if((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,10}" + HOUR + "[:时|点]" + MIN + "[分]?" + SECOND + "[秒]?").matcher(dateStr)).find()) {
			//2015/9/22 0:16:41
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
				sec = NumberUtils.parseInt(matcher.group(6));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日-]" +YEAR_2_4 +"[^0-9]{0,5}"+ HOUR + "[:时|点]" + MIN ).matcher(dateStr)).find()){
//			7/6/2014 12:26
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,5}" + HOUR + "[:时|点]" + MIN + "[:分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
//			7/6/ 12:26:12
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				hour = NumberUtils.parseInt(matcher.group(3));
				min = NumberUtils.parseInt(matcher.group(4));
				sec = NumberUtils.parseInt(matcher.group(5));
				year = DateUtils.getSysdateYear();
				int sysdateMonth = DateUtils.getSysdateMonth();
				if (month > sysdateMonth){
					year = year-1;
				}else if (month == sysdateMonth && day> DateUtils.getSysdateDay()){
					year = year-1;
				}
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,5}" + HOUR + "[:时|点]" + MIN + "[分]?").matcher(dateStr)).find()){
//			7/6/ 12:26
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				hour = NumberUtils.parseInt(matcher.group(3));
				min = NumberUtils.parseInt(matcher.group(4));
				year = DateUtils.getSysdateYear();
				int sysdateMonth = DateUtils.getSysdateMonth();
				if (month > sysdateMonth){
					year = year-1;
				}else if (month == sysdateMonth && day> DateUtils.getSysdateDay()){
					year = year-1;
				}
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?[^0-9]{0,5}" + HOUR + "[:时|点]" + MIN + "[分]?").matcher(dateStr)).find()){
//			0706 12:26
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				hour = NumberUtils.parseInt(matcher.group(3));
				min = NumberUtils.parseInt(matcher.group(4));
				year = DateUtils.getSysdateYear();
				int sysdateMonth = DateUtils.getSysdateMonth();
				if (month > sysdateMonth){
					year = year-1;
				}else if (month == sysdateMonth && day> DateUtils.getSysdateDay()){
					year = year-1;
				}
			}else if ((matcher = pattern(YEAR_4+"[年| |-]"+MONTH_2+"[月| |-]"+DAY_2).matcher(dateStr)).find()){
				//2013-01-05
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2).matcher(dateStr)).find()){
				//2013-1-5
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+"[年| |-]"+MONTH_2+""+DAY_2).matcher(dateStr)).find()){
				//2013/0105
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+""+MONTH_2+"[月| |-]"+DAY_2).matcher(dateStr)).find()){
				//201301-05
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+""+MONTH_2+""+DAY_2).matcher(dateStr)).find()){
				//20130105
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+""+MONTH_1_2+""+DAY_2).matcher(dateStr)).find()){
				//2013515
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(DAY_2+"-"+MONTH_2+"-"+  YEAR_4).matcher(dateStr)).find()){
				day = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+"-"+MONTH_2+"-"+DAY_2).matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+"-"+DAY_2+"-"+MONTH_2).matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(3));
				day = NumberUtils.parseInt(matcher.group(2));
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日-]" +YEAR_2_4).matcher(dateStr)).find()){
//			7/6/2014
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_2_4 +MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				year = DateUtils.getSysdateYear();
				int sysdateMonth = DateUtils.getSysdateMonth();
				if (month > sysdateMonth){
					year = year-1;
				}else if (month == sysdateMonth && day> DateUtils.getSysdateDay()){
					year = year-1;
				}
//			Jun 17 18:24:56 2014
			}else if ((matcher = pattern("(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*[0-9]{1,2}\\s*"+HOUR + "[:时]"+MIN + "[:分]" + SECOND + "\\s*[0-9]{4}").matcher(dateStr)).find()){

				int groupCount = matcher.groupCount();
				try {
					Date dTmp = new Date(matcher.group(0));
					return dTmp.getTime();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					return null;
				}
			}else if ((matcher = pattern("(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*[0-9]{1,2}\\s*,\\s*[0-9]{4}").matcher(dateStr)).find()
							||(matcher = pattern("(?i)\\s*[0-9]{1,2}\\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*[0-9]{4}").matcher(dateStr)).find()
							||(matcher = pattern("(?i)\\s*[0-9]{4}\\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*[0-9]{1,2}").matcher(dateStr)).find()
							){

				try {
					Date dTmp = new Date(matcher.group(0)+" EST");
					return dTmp.getTime();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					return null;
				}
			}else if((matcher = P_DATE_CHINESE_SPECIAL.matcher(dateStr)).find()){
				Calendar calendar = Calendar.getInstance();
				String hourStr = matcher.group(1);
				int num = NumberUtils.matchNumber(hourStr);
				calendar.add(Calendar.HOUR, -num);
				String minStr = matcher.group(2);
				num = NumberUtils.matchNumber(minStr);
				calendar.add(Calendar.MINUTE, -num);
				return calendar.getTimeInMillis();
			}else  if ((matcher = P_DATE_CHINESE.matcher(dateStrOri)).find()){
				String str = matcher.group(1);
				Calendar calendar = Calendar.getInstance();
				if (str.indexOf("半小时前")!=-1){
					calendar.add(Calendar.MINUTE, -30);
				}else if (str.indexOf("半年前")!=-1){
					calendar.add(Calendar.DAY_OF_YEAR, -(365/2));
				}else if (str.equals("刚刚")){

				}else if(str.indexOf("秒前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.SECOND, -num);
				}else if(str.indexOf("分钟前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.MINUTE, -num);
				}else if(str.indexOf("分前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.MINUTE, -num);
				}else if(str.indexOf("小时前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.HOUR, -num);
				}else if(str.equals("昨天")){
					calendar.add(Calendar.DATE, -1);
				}else if(str.equals("前天")){
					calendar.add(Calendar.DATE, -2);
				}else if(str.indexOf("天前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.DATE, -num);
				}else if(str.indexOf("周前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.DATE, -(num*7));
				}else if(str.indexOf("星期前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.DATE, -(num*7));
				}else if(str.indexOf("月前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.MONTH, -num);
				}else if(str.indexOf("年前")!=-1){
					int num = NumberUtils.matchNumber(str);
					if(num>99){
						return null;
					}
					calendar.add(Calendar.YEAR, -num);
				}
				year = calendar.get(Calendar.YEAR);
				month = calendar.get(Calendar.MONTH);
				day = calendar.get(Calendar.DAY_OF_MONTH);

				if ((matcher = pattern(HOUR + "[:时]" + MIN + "[:分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
					hour = NumberUtils.parseInt(matcher.group(1));
					min = NumberUtils.parseInt(matcher.group(2));
					sec = NumberUtils.parseInt(matcher.group(3));
					calendar.set(Calendar.HOUR_OF_DAY, hour);
					calendar.set(Calendar.MINUTE, min);
					calendar.set(Calendar.SECOND, sec);
				}else if ((matcher = pattern(HOUR + "[:时]" + MIN + "[分]?").matcher(dateStr)).find()){
					hour = NumberUtils.parseInt(matcher.group(1));
					min = NumberUtils.parseInt(matcher.group(2));
					calendar.set(Calendar.HOUR_OF_DAY, hour);
					calendar.set(Calendar.MINUTE, min);
				}else if ((matcher = pattern("上午"+HOUR+"[:时]" + MIN + "[分]?").matcher(dateStr)).find()){
					hour = NumberUtils.parseInt(matcher.group(1));
					min = NumberUtils.parseInt(matcher.group(2));
					calendar.set(Calendar.HOUR_OF_DAY, hour);
					calendar.set(Calendar.MINUTE, min);
				}else if ((matcher = pattern("下午"+HOUR+"[:时]" + MIN + "[分]?").matcher(dateStr)).find()){
					hour = NumberUtils.parseInt(matcher.group(1));
					min = NumberUtils.parseInt(matcher.group(2));
					hour = hour + 12;
					calendar.set(Calendar.HOUR_OF_DAY, hour);
					calendar.set(Calendar.MINUTE, min);
				}
				return calendar.getTimeInMillis();

			}else if ((matcher = pattern(HOUR + "[:时]" + MIN + "[:分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
				hour = NumberUtils.parseInt(matcher.group(1));
				min = NumberUtils.parseInt(matcher.group(2));
				sec = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(HOUR + "[:]" + MIN ).matcher(dateStr)).find()){
				hour = NumberUtils.parseInt(matcher.group(1));
				min = NumberUtils.parseInt(matcher.group(2));
			}else if ((matcher = pattern("(1|2|3|4|5|6|7|8|9|10|11|12|一|二|三|四|五|六|七|八|九|十|十一|十二)[月]"+DAY_1_2+"[,]"+YEAR_2_4).matcher(dateStr)).find()){
				//七月 11, 2010
				String groupMonth = matcher.group(1);
				if(groupMonth.matches("[一|二|三|四|五|六|七|八|九|十|十一|十二]")){
					month = ChineseNumberParser.parseChineseNumber(groupMonth);
				}else{
					month = NumberUtils.parseInt(groupMonth);
				}
				year = NumberUtils.parseInt(matcher.group(3));
				day = NumberUtils.parseInt(matcher.group(2));
			}else{
				return null;
			}
			}
//			System.out.println("符合正则:"+matcher.pattern());
			if(year!=null&&year<1000){
				year += 2000;
				if (year>DateUtils.getSysdateYear()){
					year = year - 100;
				}
			}
			if(month>0){
				month = month-1;
			}


			if (year!=null)	{
				cal.set(Calendar.YEAR, year);
				cal.set(Calendar.MONTH, month);
				cal.set(Calendar.DAY_OF_MONTH, day);
			}
			if(hour!= null ){
				cal.set(Calendar.HOUR_OF_DAY, hour);
			}
			if(min!=null ){
				cal.set(Calendar.MINUTE, min);
			}
			if(sec!=null){
				cal.set(Calendar.SECOND, sec);
			}
			long timeInMillis = cal.getTimeInMillis();
			if (timeInMillis<=0){
//				System.err.println("解析结果时间小于1970，可能有误 ["+ StringUtils.left(dateStrOri,50)+"]" + DateUtils.format(timeInMillis,"yyyy/MM/dd HH:mm:ss"));
				return null;
			}
			if (timeInMillis > System.currentTimeMillis()+3600l*1000*24){
				System.err.println("解析结果超过当前时间加24小时，可能有误：" + StringUtils.left(dateStrOri,50) +" ->" + DateUtils.format(timeInMillis,"yyyy/MM/dd HH:mm:ss"));
				return null;
			}
			return timeInMillis;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 重载方法，返回日期和Matcher，用于截取文章
	 * @param dateStrOri
	 * @return
	 */
	public static Map<Long,Matcher> matchDate2(String dateStrOri){
		Map<Long,Matcher> map = new HashMap<Long,Matcher>();
		if(StringUtils.isNullOrEmpty(dateStrOri)){
			return null;
		}

		try {
			Integer year = null;
			int month = 0;
			int day = 0;
			Integer hour = null;
			Integer min = null;
			Integer sec = null;
			Calendar cal = Calendar.getInstance();


			Matcher matcher = null;

			if ((matcher = DATE_CHINESE.matcher(dateStrOri)).find()){
				//二〇〇八年三月三十一日，二〇一二年十二月二十六日
				String yearStr = matcher.group(1);
				String monthStr = matcher.group(2);
				String dayStr = matcher.group(3);
				year = ChineseNumberParser.parseChineseNumber(yearStr);
				month = ChineseNumberParser.parseChineseNumber(monthStr);
				day = ChineseNumberParser.parseChineseNumber(dayStr);
				if (year==-1 || month==-1 || day==-1){
					System.out.println("中文日期解析错误：" + yearStr + "年" +monthStr +"月"+dayStr + "日"  );
					return null;
				}
			}else{
			String dateStr = formatMatchDateString(dateStrOri);

			if ((matcher = pattern(DAY_1_2+"[月| |-]"+MONTH_1_2+"[日|-]"+YEAR_2_4 +"[^0-9]{0,3}(AM|PM)[^0-9]{0,3}"+ HOUR + "[:|时|点]" + MIN + "[:|分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
				//21/1/2013 PM 4:36:45　
				day = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				String ampm = matcher.group(4);
				hour = NumberUtils.parseInt(matcher.group(5));
				min = NumberUtils.parseInt(matcher.group(6));
				sec = NumberUtils.parseInt(matcher.group(7));
				if ("PM".equals(ampm)){
					hour = 12 + hour;
				}
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,3}(上午|下午)[^0-9]{0,3}" + HOUR + "[:时|点]" + MIN + "[分]?").matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				String sxw = matcher.group(4);
				hour = NumberUtils.parseInt(matcher.group(5));
				min = NumberUtils.parseInt(matcher.group(6));
				if ("下午".equals(sxw)){
					hour = 12 + hour;
				}
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,10}"+ HOUR + "[:|时|点]" + MIN + "[:|分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
//			14-7-6 12:26:21
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
				sec = NumberUtils.parseInt(matcher.group(6));
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,10}" + HOUR + "[:时|点]" + MIN + "[分]?").matcher(dateStr)).find()){
//			14-7-6 12:26
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern(YEAR_4 +"[年| |-]?"+MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?[^0-9]{0,10}"+ HOUR + "[:|时|点]" + MIN + "[:|分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
//			201476 12:26:01
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
				sec = NumberUtils.parseInt(matcher.group(6));
			}else if ((matcher = pattern("上午"+ HOUR + "[:|时|点]" + MIN + "[:|分]?" +"[^0-9]{0,10}"+YEAR_4 +"[年| |-]?"+MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
//				上午8:35 - 2015年10月12日
				hour = NumberUtils.parseInt(matcher.group(1));
				min = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				month = NumberUtils.parseInt(matcher.group(4));
				day = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern("下午"+ HOUR + "[:|时|点]" + MIN + "[:|分]?" +"[^0-9]{0,10}"+YEAR_4 +"[年| |-]?"+MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
//				上午8:35 - 2015年10月12日
				hour = NumberUtils.parseInt(matcher.group(1));
				hour = hour+12;
				min = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				month = NumberUtils.parseInt(matcher.group(4));
				day = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern(YEAR_4 +"[年| |-]?"+MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?[^0-9]{0,10}"+ HOUR + "[:|时|点]" + MIN ).matcher(dateStr)).find()){
//			201476 12:26
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日-]" +YEAR_4 +"[^0-9]{0,10}"+ HOUR + "[:时|点]" + MIN + "[:分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
//			7/6/2014 12:26:01
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
				sec = NumberUtils.parseInt(matcher.group(6));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日-]" +YEAR_2_4 +"[^0-9]{0,5}"+ HOUR + "[:时|点]" + MIN ).matcher(dateStr)).find()){
//			7/6/2014 12:26
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
				hour = NumberUtils.parseInt(matcher.group(4));
				min = NumberUtils.parseInt(matcher.group(5));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,5}" + HOUR + "[:时|点]" + MIN + "[:分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
//			7/6/ 12:26:12
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				hour = NumberUtils.parseInt(matcher.group(3));
				min = NumberUtils.parseInt(matcher.group(4));
				sec = NumberUtils.parseInt(matcher.group(5));
				year = DateUtils.getSysdateYear();
				int sysdateMonth = DateUtils.getSysdateMonth();
				if (month > sysdateMonth){
					year = year-1;
				}else if (month == sysdateMonth && day> DateUtils.getSysdateDay()){
					year = year-1;
				}
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?[^0-9]{0,5}" + HOUR + "[:时|点]" + MIN + "[分]?").matcher(dateStr)).find()){
//			7/6/ 12:26
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				hour = NumberUtils.parseInt(matcher.group(3));
				min = NumberUtils.parseInt(matcher.group(4));
				year = DateUtils.getSysdateYear();
				int sysdateMonth = DateUtils.getSysdateMonth();
				if (month > sysdateMonth){
					year = year-1;
				}else if (month == sysdateMonth && day> DateUtils.getSysdateDay()){
					year = year-1;
				}
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]?"+DAY_1_2+"[日]?[^0-9]{0,5}" + HOUR + "[:时|点]" + MIN + "[分]?").matcher(dateStr)).find()){
//			0706 12:26
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				hour = NumberUtils.parseInt(matcher.group(3));
				min = NumberUtils.parseInt(matcher.group(4));
				year = DateUtils.getSysdateYear();
				int sysdateMonth = DateUtils.getSysdateMonth();
				if (month > sysdateMonth){
					year = year-1;
				}else if (month == sysdateMonth && day> DateUtils.getSysdateDay()){
					year = year-1;
				}
			}else if ((matcher = pattern(YEAR_4+"[年| |-]"+MONTH_2+"[月| |-]"+DAY_2).matcher(dateStr)).find()){
				//2013-01-05
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2).matcher(dateStr)).find()){
				//2013-1-5
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+"[年| |-]"+MONTH_2+""+DAY_2).matcher(dateStr)).find()){
				//2013/0105
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+""+MONTH_2+"[月| |-]"+DAY_2).matcher(dateStr)).find()){
				//201301-05
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+""+MONTH_2+""+DAY_2).matcher(dateStr)).find()){
				//20130105
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+""+MONTH_1_2+""+DAY_2).matcher(dateStr)).find()){
				//2013515
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(DAY_2+"-"+MONTH_2+"-"+  YEAR_4).matcher(dateStr)).find()){
				day = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+"-"+MONTH_2+"-"+DAY_2).matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_4+"-"+DAY_2+"-"+MONTH_2).matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(3));
				day = NumberUtils.parseInt(matcher.group(2));
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_2_4 +"[年| |-]"+MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日-]" +YEAR_2_4).matcher(dateStr)).find()){
//			7/6/2014
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				year = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(YEAR_2_4 +MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
				year = NumberUtils.parseInt(matcher.group(1));
				month = NumberUtils.parseInt(matcher.group(2));
				day = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(MONTH_1_2+"[月| |-]"+DAY_1_2+"[日]?").matcher(dateStr)).find()){
				month = NumberUtils.parseInt(matcher.group(1));
				day = NumberUtils.parseInt(matcher.group(2));
				year = DateUtils.getSysdateYear();
				int sysdateMonth = DateUtils.getSysdateMonth();
				if (month > sysdateMonth){
					year = year-1;
				}else if (month == sysdateMonth && day> DateUtils.getSysdateDay()){
					year = year-1;
				}
//			Jun 17 18:24:56 2014
			}else if ((matcher = pattern("(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*[0-9]{1,2}\\s*"+HOUR + "[:时]"+MIN + "[:分]" + SECOND + "\\s*[0-9]{4}").matcher(dateStr)).find()){

				int groupCount = matcher.groupCount();
				try {
					Date dTmp = new Date(matcher.group(0));
					map.put(dTmp.getTime(), matcher);
					return map;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					return null;
				}
			}else if ((matcher = pattern("(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*[0-9]{1,2}\\s*,\\s*[0-9]{4}").matcher(dateStr)).find()
							||(matcher = pattern("(?i)\\s*[0-9]{1,2}\\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*[0-9]{4}").matcher(dateStr)).find()
							||(matcher = pattern("(?i)\\s*[0-9]{4}\\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*[0-9]{1,2}").matcher(dateStr)).find()
							){

				try {
					Date dTmp = new Date(matcher.group(0)+" EST");
					map.put(dTmp.getTime(), matcher);
					return map;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					return null;
				}
			}else if((matcher = P_DATE_CHINESE_SPECIAL.matcher(dateStr)).find()){
				Calendar calendar = Calendar.getInstance();
				String hourStr = matcher.group(1);
				int num = NumberUtils.matchNumber(hourStr);
				calendar.add(Calendar.HOUR, -num);
				String minStr = matcher.group(2);
				num = NumberUtils.matchNumber(minStr);
				calendar.add(Calendar.MINUTE, -num);
				map.put(calendar.getTimeInMillis(), matcher);
				return map;
			}
			else  if ((matcher = P_DATE_CHINESE.matcher(dateStrOri)).find()){
				String str = matcher.group(1);
				Calendar calendar = Calendar.getInstance();
				if (str.indexOf("半小时前")!=-1){
					calendar.add(Calendar.MINUTE, -30);
				}else if (str.indexOf("半年前")!=-1){
					calendar.add(Calendar.DAY_OF_YEAR, -(365/2));
				}else if (str.equals("刚刚")){

				}else if(str.indexOf("秒前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.SECOND, -num);
				}else if(str.indexOf("分钟前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.MINUTE, -num);
				}else if(str.indexOf("分前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.MINUTE, -num);
				}else if(str.indexOf("小时前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.HOUR, -num);
				}else if(str.equals("昨天")){
					calendar.add(Calendar.DATE, -1);
				}else if(str.equals("前天")){
					calendar.add(Calendar.DATE, -2);
				}else if(str.indexOf("天前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.DATE, -num);
				}else if(str.indexOf("周前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.DATE, -(num*7));
				}else if(str.indexOf("星期前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.DATE, -(num*7));
				}else if(str.indexOf("月前")!=-1){
					int num = NumberUtils.matchNumber(str);
					calendar.add(Calendar.MONTH, -num);
				}else if(str.indexOf("年前")!=-1){
					int num = NumberUtils.matchNumber(str);
					if(num>99){
						return null;
					}
					calendar.add(Calendar.YEAR, -num);
				}
				year = calendar.get(Calendar.YEAR);
				month = calendar.get(Calendar.MONTH);
				day = calendar.get(Calendar.DAY_OF_MONTH);

				if ((matcher = pattern(HOUR + "[:时]" + MIN + "[:分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
					hour = NumberUtils.parseInt(matcher.group(1));
					min = NumberUtils.parseInt(matcher.group(2));
					sec = NumberUtils.parseInt(matcher.group(3));
					calendar.set(Calendar.HOUR_OF_DAY, hour);
					calendar.set(Calendar.MINUTE, min);
					calendar.set(Calendar.SECOND, sec);
				}else if ((matcher = pattern(HOUR + "[:时]" + MIN + "[分]?").matcher(dateStr)).find()){
					hour = NumberUtils.parseInt(matcher.group(1));
					min = NumberUtils.parseInt(matcher.group(2));
					calendar.set(Calendar.HOUR_OF_DAY, hour);
					calendar.set(Calendar.MINUTE, min);
				}else if ((matcher = pattern("上午"+HOUR+"[:时]" + MIN + "[分]?").matcher(dateStr)).find()){
					hour = NumberUtils.parseInt(matcher.group(1));
					min = NumberUtils.parseInt(matcher.group(2));
					calendar.set(Calendar.HOUR_OF_DAY, hour);
					calendar.set(Calendar.MINUTE, min);
				}else if ((matcher = pattern("下午"+HOUR+"[:时]" + MIN + "[分]?").matcher(dateStr)).find()){
					hour = NumberUtils.parseInt(matcher.group(1));
					min = NumberUtils.parseInt(matcher.group(2));
					hour = hour + 12;
					calendar.set(Calendar.HOUR_OF_DAY, hour);
					calendar.set(Calendar.MINUTE, min);
				}
				map.put(calendar.getTimeInMillis(), matcher);
				return map;

			}else if ((matcher = pattern(HOUR + "[:时]" + MIN + "[:分]" + SECOND + "[秒]?").matcher(dateStr)).find()){
				hour = NumberUtils.parseInt(matcher.group(1));
				min = NumberUtils.parseInt(matcher.group(2));
				sec = NumberUtils.parseInt(matcher.group(3));
			}else if ((matcher = pattern(HOUR + "[:]" + MIN ).matcher(dateStr)).find()){
				hour = NumberUtils.parseInt(matcher.group(1));
				min = NumberUtils.parseInt(matcher.group(2));
			}else if ((matcher = pattern("(1|2|3|4|5|6|7|8|9|10|11|12|一|二|三|四|五|六|七|八|九|十|十一|十二)[月]"+DAY_1_2+"[,]"+YEAR_2_4).matcher(dateStr)).find()){
				//七月 11, 2010
				String groupMonth = matcher.group(1);
				if(groupMonth.matches("[一|二|三|四|五|六|七|八|九|十|十一|十二]")){
					month = ChineseNumberParser.parseChineseNumber(groupMonth);
				}else{
					month = NumberUtils.parseInt(groupMonth);
				}
				year = NumberUtils.parseInt(matcher.group(3));
				day = NumberUtils.parseInt(matcher.group(2));
			}else{
				return null;
			}
			}
//			System.out.println("符合正则:"+matcher.pattern());
			if(year!=null&&year<1000){
				year += 2000;
				if (year>DateUtils.getSysdateYear()){
					year = year - 100;
				}
			}
			if(month>0){
				month = month-1;
			}


			if (year!=null)	{
				cal.set(Calendar.YEAR, year);
				cal.set(Calendar.MONTH, month);
				cal.set(Calendar.DAY_OF_MONTH, day);
			}
			if(hour!= null ){
				cal.set(Calendar.HOUR_OF_DAY, hour);
			}
			if(min!=null ){
				cal.set(Calendar.MINUTE, min);
			}
			if(sec!=null){
				cal.set(Calendar.SECOND, sec);
			}
			long timeInMillis = cal.getTimeInMillis();
			if (timeInMillis<=0){
//				System.err.println("解析结果时间小于1970，可能有误 ["+ StringUtils.left(dateStrOri,50)+"]" + DateUtils.format(timeInMillis,"yyyy/MM/dd HH:mm:ss"));
				return null;
			}
			if (timeInMillis > System.currentTimeMillis()+3600l*1000*24){
				System.err.println("解析结果超过当前时间加24小时，可能有误：" + StringUtils.left(dateStrOri,50) +" ->" + DateUtils.format(timeInMillis,"yyyy/MM/dd HH:mm:ss"));
				return null;
			}
			map.put(timeInMillis, matcher);
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private static Pattern pattern(String string) {
		return Pattern.compile(string);
	}
	static int getSysdateYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}
	public static String getNowTime(int pd) {
		String time = null;
		if (pd == 1) {// long
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(String.valueOf(calendar.get(calendar.YEAR))
					+ "-" + String.valueOf(calendar.get(calendar.MONTH) + 1)
					+ "-" + String.valueOf(calendar.get(calendar.DATE)) + " "
					+ String.valueOf(calendar.get(calendar.HOUR)) + ":"
					+ String.valueOf(calendar.get(calendar.MINUTE)) + ":"
					+ String.valueOf(calendar.get(calendar.SECOND)));
		} else if (pd == 10) {// short date
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(String.valueOf(calendar.get(calendar.YEAR))
					+ "-" + String.valueOf(calendar.get(calendar.MONTH) + 1)
					+ "-" + String.valueOf(calendar.get(calendar.DATE)));
		} else if (pd == 2) {// time
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(String.valueOf(calendar.get(calendar.HOUR))
					+ ":" + String.valueOf(calendar.get(calendar.MINUTE)) + ":"
					+ String.valueOf(calendar.get(calendar.SECOND)));
		} else if (pd == 4) {// shortime
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(String.valueOf(calendar.get(calendar.HOUR))
					+ ":" + String.valueOf(calendar.get(calendar.MINUTE)));
		} else if (pd == 5) {// 获得小时
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(calendar.get(calendar.HOUR_OF_DAY));
		} else if (pd == 6) {// 获得分钟
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(calendar.get(calendar.MINUTE));
		} else if (pd == 7) {// 获得秒
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(calendar.get(calendar.SECOND));
		} else if (pd == 8) {// 获得毫秒
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(calendar.get(calendar.MILLISECOND));
		} else if (pd == 9) {// long
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(String.valueOf(calendar.get(calendar.YEAR))
					+ "-" + String.valueOf(calendar.get(calendar.MONTH) + 1)
					+ "-" + String.valueOf(calendar.get(calendar.DATE)) + " "
					+ String.valueOf(calendar.get(calendar.HOUR_OF_DAY)) + ":"
					+ String.valueOf(calendar.get(calendar.MINUTE)) + ":"
					+ String.valueOf(calendar.get(calendar.SECOND)));
		} else if (pd == 10) {// 获得一周的第几天
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(calendar.get(calendar.DAY_OF_WEEK));
		} else if (pd == 11) {// 月中的第几天
			Date date = new Date();
			GregorianCalendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			time = String.valueOf(String.valueOf(calendar
					.get(calendar.DAY_OF_MONTH)));
		}
		return time;
	}




	public static void testMatchDate(String string) {
		string = DateUtils.removeWeiyu(string);
//		System.out.println(string);
		Long matchDate = matchDate(string);
		if (matchDate!=null){
			System.out.println(DateUtils.format(matchDate,"yyyy/MM/dd HH:mm:ss") + "\t" + string);
		}else{
			System.err.println("解析失败：" + string);
		}

	}

	public static int getSysdateDay() {
		return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
	}

	public static int getSysdateMonth(){
		return Calendar.getInstance().get(Calendar.MONTH) + 1;
	}

	public static boolean isToday(Long date) {
		if (date==null) return false;
		Calendar sysdate = Calendar.getInstance();
		Calendar dateCal = Calendar.getInstance();
		dateCal.setTimeInMillis(date);
//		System.out.println(format(date,"yyyy/MM/dd HH:mm:ss"));
		return (sysdate.get(Calendar.YEAR) == dateCal.get(Calendar.YEAR))
				&& (sysdate.get(Calendar.DAY_OF_YEAR) == dateCal.get(Calendar.DAY_OF_YEAR));
	}

	public static String formatMatchDateString(String str) {
		str = StringUtils.ToDBC(str);
		str = str.replace("&nbsp;", " ");
		str = str.replaceAll("[^0-9a-zA-Z:,一二三四五六七八九十\\-/今昨半个年月日周星期天小时分钟秒前月前上下午]", " ");
		str = str.replaceAll("[ ]+", " ");
		str = str.replaceAll("[ ]*[/\\-][ ]*", "-");
		str = str.replaceAll("[ ]*[/\\-][ ]*", "-");
//		System.out.println(str);
		return str;
	}

	public static String removeWeiyu(String str) {
		if (str==null)return null;
		StringBuffer sb =new StringBuffer();
		for (int j = 0; j < str.length(); j++) {
				char c = str.charAt(j);
				Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
				 if ( ub == Character.UnicodeBlock.ARABIC
				            || ub == Character.UnicodeBlock.ARABIC_PRESENTATION_FORMS_A
				            || ub == Character.UnicodeBlock.ARABIC_PRESENTATION_FORMS_B
				            || ub == Character.UnicodeBlock.TIBETAN
				            ) {
					 continue;
			}else{
				sb.append(c);
			}
		}
		return sb.toString();
	}

	public static void main(String[] args) {
//		System.out.println(DateUtils.format(matchDate(" 2015-9-22 0:16 "),"yyyy/MM/dd HH:mm:ss"));
//		testMatchDate(" 2012-02-13 13:20 ");
		testMatchDate("2016年11月10日 07:08 AM");
//		SimpleDateFormat sdf2 = new SimpleDateFormat("d MMM yyyy",Locale.ENGLISH);
//		String date ="31 August 2014";
//		try {
//			Date parse = sdf2.parse(date);
//			System.out.println(sdf2.format(parse));
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		SecureRandom ng = new SecureRandom();
//		long date = System.currentTimeMillis() - StringUtils.getIntRandom(Integer.MAX_VALUE);
//		date = date - StringUtils.getIntRandom(Integer.MAX_VALUE);
//		date = date - StringUtils.getIntRandom(Integer.MAX_VALUE);
//		date = date - StringUtils.getIntRandom(Integer.MAX_VALUE);
//		System.out.println(DateUtils.format(date,"yyyy/MM/dd HH:mm:ss"));
//		System.out.println(format(1402021586055l, "yyyy/MM/dd HH:mm:ss"));
//		System.out.println(isToday(1395800143l *1000));
//		<td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
//		 <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
//		try {
//			Date parse = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",Locale.US).parse("Wed Apr 16 18:58:11 +0800 2014");
//			System.out.println(format(parse,"yyyy/MM/dd HH:mm:ss"));
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println(format(1393292441l * 1000,"yyyy/MM/dd HH:mm:ss"));
//		Date parse = parse("Wed Apr 16 18:58:11 +0800 2014", "EEE MMM dd HH:mm:ss Z yyyy");
//		System.out.println(format(parse,"yyyy/MM/dd HH:mm:ss"));
//		testMatchDate("Wed Apr 16 18:58:11 +0800 2014");
//		testMatchDate("上午8:35 - 2015年10月12日");
//		testMatchDate("下午10:13 - 2015年10月12日");
//		testMatchDate("2015- يىل 3- سېنتەبىر 21:50");
//		testMatchDate(("يازما ۋاقتى: 2015- يىل 6- مارت "));
//		testMatchDate(("قتى :2014-يىلى06-ئاينىڭ23-كۈنى "));
//		testMatchDate(("ۋاقىت : 17-07-2014 تەھرىرى : "));
//		testMatchDate("October 10, 2014, 5:40 PM");
//		testMatchDate("时间:[2014-7-7] ");
//		testMatchDate("八月 26, 2014".replace("八", "8").replace(" ", ""));
//		testMatchDate("http://haixi.cnfol.com/baodaowanxiang/20140723/18474664.shtml");
//		testMatchDate("更新时间 2014年7月11日, 格林尼治标准时间08:17 ");
//		testMatchDate("00:00/00:00 3小时前 上传 1小时前");
//		testMatchDate(" 2012年02月13日 13时20分11秒 ");
//		testMatchDate("1小时30分钟前");
//		testMatchDate("20140710 09:28");
//		testMatchDate("今天 00:44 ");
//		testMatchDate("7/6/2014 12:26:00 PM ");
//		testMatchDate("Tue Jun 17 18:24:56 2014");
//		testMatchDate(" 06/23/2014");
//		testMatchDate(" 2012-02-13 13:20 ");
//		testMatchDate("2015-9-22 0:16:41");
//		testMatchDate(" 2012年02-13 ");
//		testMatchDate(" 1999-02-13 ");
//		testMatchDate(" 12-02-13 ");
//		testMatchDate(" 12年02月13日 ");
//		testMatchDate(" 02月13 ");
//		testMatchDate(" 02-13 ");
//		testMatchDate(" 2012年02月13 13时20分11秒 ");
//		testMatchDate("  13时20分11秒 ");
//		testMatchDate("201202-13");
//		testMatchDate("201202/13");
//		testMatchDate("发表于 Wed Sep 25 22:17:38 CST 2013");
//		testMatchDate("发布者：中心办公室  发布时间： Jun  7,   2013     【关闭】");
//		testMatchDate("20120213");
//		testMatchDate("2012513");
//		testMatchDate("//xxx/201202/13/t20120616_118124.html");
//		testMatchDate("[原创 2013-1-18 9:25:41]    ");
//		testMatchDate("年轮日线趋势跟踪系统13-01-18 [原创 2013-1-18 9:25:41]    ");
//		testMatchDate("//xxx/201206/t20120216_118124.html");
//		testMatchDate("http://www.njtv.com.cn/cont/cont_v/cont_jiangning/201206/t20120616_118124.html");
//		testMatchDate("前天 12:04");
//		testMatchDate("昨天19:56");
//		testMatchDate("昨天 12:04");
//		testMatchDate("刚刚 12:04");
//		testMatchDate("1分钟前");
//		testMatchDate("2014-08-04290391.html");
//		testMatchDate("1天前");
//		testMatchDate("1周前");
//		testMatchDate("5小时前");
//		testMatchDate("七月 11, 2010".replaceAll("\\s", ""));
//		testMatchDate("观察  clark lam  |  1小时前");
//		testMatchDate("2个月前");
//		testMatchDate("2 个月前");
//		testMatchDate("2   天前");
//		testMatchDate("2&nbsp;天前");
//		testMatchDate("2008.06.12");
//		testMatchDate("http://www.astvvideo.com/video/detail/kpas/201112/20/6ecb9baf28dc47808a334bef8acf7496.html");
//		testMatchDate("发表于：98-08-09 18:07");
//		testMatchDate("http://news.tsbtv.tv/2013/0116/6925.shtml");
//		testMatchDate("http://www.serc.gov.cn/yxjg/201301/t20130117_36735.htm");
//		testMatchDate("http://news.iqilu.com/shandong/shandonggedi/20130116/1426678.shtml");
//		testMatchDate("http://focus.scol.com.cn/xxsd/content/2013-01/16/content_4603622.htm?node=514");
//		testMatchDate("二○一二年三月二十七日");
//		testMatchDate("二〇〇八年三月三十一日，");
//		testMatchDate("二〇一二年十二月二十六日");
//		testMatchDate("二O一二年十月二十三日");
//		testMatchDate("二〇一二年七月九日");
//		testMatchDate("一二年七月九日");
//		testMatchDate("二〇〇九年十月十四日");
//		testMatchDate("撰写时间：21/1/2013 PM 4:36:45");
//		testMatchDate("2011-6-30");
//		testMatchDate("http://news.gxtv.cn/201310/news_1193217311.html");
//		testMatchDate("2025年前");
//		testMatchDate("25年前");
//		testMatchDate("半年前");
//		testMatchDate("2015 ལོའི་ཟླ /1 ཚེས -15 ཉིན");
//		testMatchDate("楼主：马小星2013 时间：2015-02-27 16:22:00 ");
//		testMatchDate("2014/02/04下午 2:26");
//		testMatchDate("2015-6-16 11:10");

	}

	public static Long parseLong(String date, String pattern, Locale locale) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern,locale);
		if(date!=null){
			try {
				Date parse = sdf.parse(date);
				if(parse!=null) return parse.getTime();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
